package Control;

import Model.DBUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class MainView extends Application {
    //用来判断是否加载了图片
    int inInt = 0;
    int outInt = 0;
    //增加变量来判断数据库的行为是否成功
    public static int currentActionInt;
    //用该变量来记录当前做行为的车辆名称
    public static String currentCarName;
    //用该变量记录当前的剩余车位数量
    public static int currentPlace = 32;
    //用来延迟显示状态
    public static int flags = 0;
    File imageFile;

    Image currentImage;

    AnchorPane root;

    ObservableList<Car> observable;

    @FXML
    private Button btnNewRreash;

    @FXML
    private Label systemState;

    @FXML
    private Button btnRefresh;

    @FXML
    private ImageView FXimage;

    @FXML
    private Button btnHistory;

    @FXML
    private ToggleButton btnSwitch;

    @FXML
    private Label textState;

    @FXML
    private Label textFreePark;

    @FXML
    private TableView<Car> tableMainView;

    @FXML
    private TableColumn<Car, String> tableID;

    @FXML
    private TableColumn<Car, String> tableInTime;

    @FXML
    private TableColumn<Car, String> tableOutTime;

    @FXML
    private TableColumn<Car, String> tableState;

    @FXML
    private Button btnUpPage;

    @FXML
    private Button btnDownPage;

    @FXML
    private Button btnModifyData;

    @FXML
    void Refresh(ActionEvent event) throws IOException {
        this.showList();
        systemState.setText("运行中");
        this.showImage();
    }

    @FXML
    void btnNewRreash(ActionEvent event) {
        System.out.println("刷新数据成功");
        MyRunnable.clearCarHashSet();
    }

    @FXML
    void btnDownPage(ActionEvent event) {

    }

    @FXML
    void btnHistory(ActionEvent event) throws IOException {
        HistoryCode.showView();
    }

    @FXML
    void btnModifyData(ActionEvent event) throws IOException {
        ControlView.showView();
    }

    @FXML
    void btnSwitch(ActionEvent event) {
        if (flags==0){
            flags=1;
            System.out.println("延迟状态修改为1(车辆不允许通过!检测通道关闭)");
        }else if (flags==1){
            flags=0;
            System.out.println("延迟状态修改为0(正常车辆进出)");
        }
    }

    @FXML
    void btnUpPage(ActionEvent event) {

    }

    public MainView() {

    }

    public void showList() throws IOException {
        //设置前端的tableview的组件
        //加载数据的集合
        observable = FXCollections.observableArrayList(MyRunnable.getMyrun());
        this.tableID.setCellValueFactory(new PropertyValueFactory<>("id"));
        this.tableInTime.setCellValueFactory(new PropertyValueFactory<>("timein"));
        this.tableOutTime.setCellValueFactory(new PropertyValueFactory<>("timeout"));
        this.tableState.setCellValueFactory(new PropertyValueFactory<>("state"));
        //数据线程
        //刷新显示出来数据,达到实时数据的效果
        this.tableMainView.setItems(observable);
        Thread thread = new Thread(()->{
            System.out.println("主界面车辆实时数据加载线程启动！");
            while (true) {
                try {
                    Thread.sleep(2000);
                    DBUtil.getData(MyRunnable.getCarlist());
                    observable.clear();
                    observable.addAll(FXCollections.observableArrayList(MyRunnable.getMyrun()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    public void showImage(){
        Thread thread = new Thread(()->{
            System.out.println("图片检测线程启动！");
            while (true) {
                try {
                    //车辆进入判断
                    Thread.sleep(1000);
                    imageFile = new File("inImage.jpg");
                    if (imageFile.exists()){
                        //更改界面图片和状态
                        FXimage.setImage(new Image("file:inImage.jpg"));
                        Platform.runLater(() -> {
                            textState.setText("有车辆进入");
                        });
                        inInt = 1;
                        //车辆进场的延迟，
                        Thread.sleep(8000);
                        //先将数据库里面的当前车辆数据拿出来后显示到页面,并会自动将这条车辆的信息从状态数据库删除
                        currentCarName = DBUtil.getCarResult(0);
                        Platform.runLater(() -> {
                            textFreePark.setText(""+(--currentPlace));
                            textState.setText("车牌:"+currentCarName);
                        });
                        Thread.sleep(1000);
                        //将数据添加进数据库
                        System.out.println("进入的车辆信息正加载进管理数据库");
                        currentActionInt = DBUtil.addCarTable(currentCarName);
                        Thread.sleep(3000);
                        //判断数据库的加载是否成功
                        if (currentActionInt>0){
                            Platform.runLater(() -> {
                                textState.setText(currentCarName+"的信息已加载");
                                System.out.println(currentCarName+"的信息加载进数据库成功");
                            });
                        }else {
                            Platform.runLater(() -> {
                                textState.setText(currentCarName+"的信息加载失败");
                                System.out.println(currentCarName+"的信息加载进数据库失败");
                            });
                        }
                        Thread.sleep(1000);
                    }
                    //进场和出场之间的延迟
                    Thread.sleep(1000);
                    //如果通道阀门关闭则检测系统会卡在这里
                    while (flags==1){
                        Thread.sleep(5000);
                    }
                    Platform.runLater(() -> {
                        textState.setText("正在检测");
                    });
                    FXimage.setImage(new Image("file:lisenterImages.jpg"));
                    if (inInt == 1){
                        //删除图片
                        imageFile.delete();
                        inInt = 0;
                    }
                    //车辆出场判断
                    imageFile = new File("outImage.jpg");
                    if (imageFile.exists()){
                        FXimage.setImage(new Image("file:outImage.jpg"));
                        Platform.runLater(() -> {
                            textState.setText("有车辆出场");
                        });
                        outInt = 1;
                        //先将数据库里面的当前车辆数据拿出来后显示到页面,并会自动将这条车辆的信息从状态数据库删除
                        currentCarName = DBUtil.getCarResult(1);
                        Platform.runLater(() -> {
                            textFreePark.setText(""+(++currentPlace));
                            textState.setText("车牌:"+currentCarName+"出场");
                        });
                        //将数据添加进数据库
                        System.out.println("出场的车辆信息正加载进管理数据库");
                        //更新在数据库的数据
                        DBUtil.updateOutData(currentCarName);
                        //将出场的车辆数据添加到历史记录表中
                        currentActionInt = DBUtil.addCarHistory(currentCarName);
                        Thread.sleep(3000);
                        if (currentActionInt>0){
                            Platform.runLater(() -> {
                                textState.setText(currentCarName+"出场信息加载成功");
                                System.out.println("出场的车辆信息正加载进管理数据库成功");
                            });
                        }else {
                            Platform.runLater(() -> {
                                textState.setText(currentCarName+"出场信息加载失败");
                                System.out.println("出场的车辆信息正加载进管理数据库失败");
                            });
                        }
                    }
                    //进场和出场之间的延迟
                    Thread.sleep(1000);
                    //如果通道阀门关闭则检测系统会卡在这里
                    while (flags==1){
                        Thread.sleep(5000);
                    }
                    Platform.runLater(() -> {
                        textState.setText("正在检测");
                    });
                    FXimage.setImage(new Image("file:lisenterImages.jpg"));
                    if (outInt == 1){
                        //删除图片
                        imageFile.delete();
                        outInt = 0;
                    }
                    Thread.sleep(1000);
                } catch (Exception e) {
                    System.out.println("图片进程错误");
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }


    public void showView(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        URL url = getClass().getResource("MainView.fxml");
        root = FXMLLoader.load(url);
        Scene scene = new Scene(root,625,532);
        primaryStage.setScene(scene);
        primaryStage.setTitle( "车辆管理系统");
        primaryStage.show();
    }
}

